package time.burrito.menus;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MenuGameOver extends Activity{
	
	private int marcador;
	private int marcadorMaxN1;
	private SharedPreferences prefs;
	private SharedPreferences.Editor editor;
	private TextView tv;
	private TextView aviso;
	private EditText eT;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        setContentView(R.layout.game_over);
        
        Bundle extras = getIntent().getExtras();
        marcador = extras.getInt("marcador");
        tv = (TextView)findViewById(R.id.scoretv);
        tv.setText(marcador+"");
        
        prefs = getSharedPreferences("marcadores", Context.MODE_PRIVATE);
        editor = prefs.edit();
        tv = (TextView)findViewById(R.id.felicidades);
        tv.setVisibility(View.INVISIBLE);
        eT = (EditText)findViewById(R.id.campo_score);
        eT.setVisibility(View.INVISIBLE);
        eT.setSingleLine();
        marcadorMaxN1=prefs.getInt("scoreN1", 0);
        aviso = (TextView)findViewById(R.id.warning);
        aviso.setVisibility(View.INVISIBLE);
        
        
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
        if(marcador>marcadorMaxN1){ //Si el marcador nuevo es mayor que el guardado hace todo el procedimiento del saving
        	tv.setVisibility(View.VISIBLE);
        	eT.setVisibility(View.VISIBLE);
        	((Button)findViewById(R.id.btnAceptar)).setOnClickListener(new View.OnClickListener() {
    			
    			public void onClick(View arg0) {
    				String nombre = eT.getText().toString();
    				editor.putString("userN1", nombre);
    				editor.putInt("scoreN1", marcador);
    				Intent intencion = new Intent(getBaseContext(), SeleccionarNivel.class);
    				startActivity(intencion);
    				finish();

    			}
    		});
                           
        }else{
        	((Button)findViewById(R.id.btnAceptar)).setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intencion = new Intent(getBaseContext(), SeleccionarNivel.class);
    				startActivity(intencion);
				}
			});
        }
		
		
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		
    	
        editor.commit();
        super.onStop();
	}
	

}
